package org.campuscontent.ui;

import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JTextField;

import org.campuscontent.config.Config;
import org.campuscontent.main.MainFrame;
import org.campuscontent.util.GraphInspector;

public class Template1 extends TemplateBase
{

	public static int paraID = 1;
	JTextField textAreaF;
	JTextField textAreaE;
	JTextField textAreaI;
	JTextField textAreaQ;
	JTextField textAreaS;
	String graphStrTemp;

	public Template1(MainFrame mf)
	{
		super(mf);
		JLabel labelA = new JLabel();
		JLabel labelF = new JLabel();
		textAreaF = new JTextField();
		JLabel labelE = new JLabel();
		textAreaE = new JTextField();

		JLabel labelI = new JLabel();
		textAreaI = new JTextField();

		JLabel labelQ = new JLabel();
		textAreaQ = new JTextField();

		JLabel labelS = new JLabel();
		textAreaS = new JTextField();

		labelA.setBounds(5, 5, 120, 30);
		labelF.setBounds(160, 5, 30, 30);
		textAreaF.setBounds(185, 10, 100, 20);

		labelE.setBounds(325, 5, 30, 30);
		textAreaE.setBounds(360, 10, 100, 20);

		labelI.setBounds(500, 5, 30, 30);
		textAreaI.setBounds(535, 10, 100, 20);

		labelQ.setBounds(5, 40, 30, 30);
		textAreaQ.setBounds(40, 45, 260, 20);

		labelS.setBounds(305, 40, 30, 30);
		textAreaS.setBounds(340, 45, 295, 20);

		labelA.setText("A = ( \u2211, Q, \u03B4, I, F )");
		labelF.setText("F=");
		textAreaF.setText("{ }");

		labelE.setText("\u2211 =");
		textAreaE.setText("{ }");

		labelI.setText("I =");
		textAreaI.setText("{ }");

		labelQ.setText("Q =");
		textAreaQ.setText("{ }");

		labelS.setText("\u03B4 = ");
		textAreaS.setText("{ }");

		textAreaF.setBorder(BorderFactory.createLoweredBevelBorder());
		textAreaE.setBorder(BorderFactory.createLoweredBevelBorder());
		textAreaI.setBorder(BorderFactory.createLoweredBevelBorder());
		textAreaQ.setBorder(BorderFactory.createLoweredBevelBorder());
		textAreaS.setBorder(BorderFactory.createLoweredBevelBorder());

		left.add(labelA);
		left.add(labelF);
		left.add(textAreaF);
		left.add(labelE);
		left.add(textAreaE);

		left.add(labelI);
		left.add(textAreaI);

		left.add(labelQ);
		left.add(textAreaQ);

		left.add(labelS);
		left.add(textAreaS);
		
		mf.rootPanel.setDividerLocation(Config.ROOT_HEIGHT);

		graphStrTemp = new String("");
		// this.setBorder(null);
		// this.add(paraSplitPanel,BorderLayout.CENTER);
		// this.add(paraSplitPanel);
	}

	public String checkAns()
	{
		
		
		graphInspector = new GraphInspector( mf.ae.getGraph());
		
		if(graphInspector.validate()!="valid"){
			return graphInspector.validate();			
		} 
		
		//Compare the "End Status" part
		if(!compareAns(textAreaF.getText(), graphInspector.getCellLabels("end"))){
			return "The answer is not correct, please check the input \"F\"";
		} 
		
		//Compare the "Input Symbol" part
		if(!compareAns(textAreaE.getText(), graphInspector.getCellLabels("edge"))){
			return "The answer is not correct, please check the input \"\u2211\"";
		} 
		
		//Compare the "Start Status" part
		if(!compareAns(textAreaI.getText(), graphInspector.getCellLabels("start"))){
			return "The answer is not correct, please check the input \"I\"";
		}
		
		//Compare the "Available Status" part
		if(!compareAns(textAreaQ.getText(), graphInspector.getCellLabels("node"))){
			return "The answer is not correct, please check the input \"Q\"";
		} 
		
		//Compare the "Transition" part
		if(!compareAnsPlus(textAreaS.getText(), graphInspector.getCellLabels("transition"))){
			return "The answer is not correct, please check the input \"\u03B4\"";
		} 
			
		
		return "True";	
	}

	public void showSolution()
	{
		super.showSolution();
		textAreaI.setText("{" + getAnsStr("start") + "}"); // Get the start Status
		textAreaF.setText("{" + getAnsStr("end") + "}"); // Get the end Status
		textAreaQ.setText("{" + getAnsStr("node") + "}"); // Get the status list
		textAreaE.setText("{" + getAnsStr("edge") + "}"); // Get the input list
		textAreaS.setText("{" + getAnsStr("transition") + "}"); // Get the transition list
	}

	public void setParaConfig(String str)
	{

//		textAreaI.setText("{" + getAnsStr("start") + "}"); // Get the start Status
//		textAreaF.setText("{" + getAnsStr("end") + "}"); // Get the end Status
//		textAreaQ.setText("{" + getAnsStr("node") + "}"); // Get the status list
//		textAreaE.setText("{" + getAnsStr("edge") + "}"); // Get the input list
//		textAreaS.setText("{" + getAnsStr("transition") + "}"); // Get the transition list

	}

	public String getAnsStr(String fieldName)
	{

		GraphInspector graphInspector = new GraphInspector( mf.ae.getGraph() );
		ArrayList cellList = graphInspector.getCellLabels(fieldName);

		String fieldStr = "";
		for (int i = 0; i < cellList.size(); i++)
		{
			fieldStr += cellList.get(i) + ", ";
		}

		fieldStr = fieldStr.substring(0, fieldStr.length() - 2);
		return fieldStr;
	}

	public boolean setPreview()
	{
		
//		graphInspector = new GraphInspector( mf.ae.getGraph() );
//		
//		
//		if(graphInspector.validate()!="valid"){
//			JOptionPane.showMessageDialog(getWindowForComponent(mf.applet), graphInspector.validate());
//			return false;
//		} 
//		
//		mf.ae.setEnable(true);	
//		graphStrTemp = mf.ae.getGraphAsString();
//		setParaConfig("");
//		mf.ae.loadGraphFromStr("");     // remove all the objects on AE
//		this.setDividerLocation(Config.POSITION_TO_SHOW_PROBLEM_CONTROLS);
//		mf.rootPanel.setDividerLocation(Config.POSITION_TO_SHOW_TEMPLATE);
//		
//		textAreaE.setEditable(false);
//		textAreaF.setEditable(false);
//		textAreaI.setEditable(false);
//		textAreaQ.setEditable(false);
//		textAreaS.setEditable(false);

		if (!super.setPreview()) return false;

		return true;
	}

	public boolean exitPreview()
	{
//		mf.ae.loadGraphFromStr(graphStrTemp);
		this.setDividerLocation(Config.POSITION_TO_HIDE_PROBLEM_CONTROLS);
		mf.rootPanel.setDividerLocation(Config.ROOT_HEIGHT);
		
//		textAreaE.setEditable(true);
//		textAreaF.setEditable(true);
//		textAreaI.setEditable(true);
//		textAreaQ.setEditable(true);
//		textAreaS.setEditable(true);

		
		return true;
	}
	
	public boolean compareAns(String candidateAns, ArrayList correctAns){
		
		String processedStr = candidateAns.trim();  
		
		if(processedStr.length() < 2)
			return false;
		else
			processedStr = processedStr.substring(1, processedStr.length()-1);
		
		String[] ansArr = processedStr.split(",");
		
		if(ansArr.length != correctAns.size())
			return false;
		
		for(int i = 0; i < ansArr.length; i++){
			if(correctAns.contains(ansArr[i].trim()))
				correctAns.remove(ansArr[i].trim());
			else 
				return false;
		}
		
		if(correctAns.size() > 0)
			return false;
			
		return true;
	}
	
	public boolean compareAnsPlus(String candidateAns, ArrayList correctAns){
		
		String processedStr = candidateAns.trim();  
		String currentAns;
		
		if(processedStr.length() < 2)
			return false;
		else
			processedStr = processedStr.substring(1, processedStr.length()-1).trim();
		
		String startStr = "((";
		String endStr	= ")";
		
		int startPos = processedStr.indexOf(startStr);
		int endPos;
		
		for(int i = 0; i < correctAns.size(); i++){
			String tempStr = (String) correctAns.remove(i);
			tempStr = tempStr.replaceAll(" ","");
			correctAns.add(i, tempStr);
		}
		
		while(startPos != -1){
			endPos = processedStr.indexOf(endStr, startPos);
			endPos = processedStr.indexOf(endStr, endPos + 1);
			
			currentAns = processedStr.substring(startPos, endPos+1);
			currentAns = currentAns.replaceAll(" ", "");
			
			if(correctAns.contains(currentAns))
				correctAns.remove(currentAns);
			else 
				return false;
			
			startPos = processedStr.indexOf(startStr, endPos);
			
		}
						
		if(correctAns.size() > 0)
			return false;
			
		return true;
	}

	public int getId()
	{
		return paraID;
	}
}
